<template>
    <view class="main-work" v-if="isWork">
        <view class="form-box shadow">
            <view class="form-group">
                <view class="title must">旧密码</view>
                <input maxlength="30" type="password" placeholder="请填写旧密码" v-model="formOldPassword" :focus="formOldPasswordFocus" />
            </view>
            <view v-if="formOldPasswordFocus" class="hint-desc error">{{ formOldPasswordFocus }}</view>

            <view class="form-group">
                <view class="title must">新密码</view>
                <input maxlength="30" type="password" placeholder="请填写新密码" v-model="formPassword" :focus="formPasswordFocus" />
            </view>
            <view v-if="formPasswordFocus" class="hint-desc error">{{ formPasswordFocus }}</view>

            <view class="form-group">
                <view class="title must">新密码再次填写</view>
                <input maxlength="30" type="password" placeholder="请再次填写新密码" v-model="formPassword2" :focus="formPassword2Focus" />
            </view>
<!--            <view v-if="formPassword2Focus" class="hint-desc error">{{ formPassword2Focus }}</view>-->
        </view>

        <button @tap="bindFormSubmit" class="btn-work margin-top">提交修改</button>
    </view>
</template>

<script>
import {updateWorkPwd} from "../../../../api/workapi";

const WorkBiz = require('@/utils/biz/work_biz.js');
const pageHelper = require('@/utils/helper/page_helper.js');
const cloudHelper = require('@/utils/helper/cloud_helper.js');
const validate = require('@/utils/helper/validate.js');
const api = require('@/api/workapi.js');
export default {
    data() {
        return {
            formOldPassword: '',
            formPassword: '',
            formPassword2: '',
            isWork: false,
            formOldPasswordFocus: null,
            formPasswordFocus: ["新密码不能为空", "新密码长度不能小于6位", "新密码长度不能大于30位"],
            formPassword2Focus: null
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
        if (!WorkBiz.isWork(this)) {
            return;
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    methods: {
        /**
         * 数据提交
         */
        bindFormSubmit: async function () {
            if (!WorkBiz.isWork(this)) {
                return;
            }
            let data = this;

            // 数据校验
            data = validate.check(data, WorkBiz.CHECK_FORM_MGR_PWD, this);
            if (!data) {
                return;
            }
            if (data.password !== data.password2) {
                return pageHelper.showModal('两次输入的新密码不一致');
            }
            try {
                await api.updateWorkPwd(data).then((res) => {
                    let callback = () => {
                        uni.navigateBack();
                    };
                    pageHelper.showSuccToast('修改成功', 1500, callback);
                });
            } catch (err) {
                console.log('CatchClause', err);
                console.log('CatchClause', err);
                console.log(err);
            }
        }
    }
};
</script>
<style>
@import './work_pwd.css';
</style>
